/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hwangjr.rxbus.app;

import com.hwangjr.rxbus.RxBus;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

/**
 * MainAbility
 */
public class MainAbility extends Ability {
    Button button;
    Button case2;
    ComponentContainer directionLayout;

    /**
     * CatMam to birth Tom.
     */
    private CatMam mCatMam = new CatMam();

    /**
     * MouseMam to birth mouse.
     */
    private MouseMam mMouseMam = new MouseMam();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        if (LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_main, null,
                false) instanceof ComponentContainer) {
            directionLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_layout_main, null, false);
        }
        if (directionLayout.findComponentById(ResourceTable.Id_button1) instanceof Button) {
            button = (Button) directionLayout.findComponentById(ResourceTable.Id_button1);
        }
        if (directionLayout.findComponentById(ResourceTable.Id_button2) instanceof Button) {
            case2 = (Button) directionLayout.findComponentById(ResourceTable.Id_button2);
        }
        button.setClickedListener(value -> buttonClick());
        case2.setClickedListener(value -> button2Click());
        RxBus.get().register(mMouseMam);
        RxBus.get().register(mCatMam.birth());
        super.setUIContent(directionLayout);
    }

    /**
     * buttonClick
     */
    public void buttonClick() {
        Mouse mouse = mMouseMam.birth();
        mouse.squeak();
        showToast(mouse);
    }

    /**
     * case 2
     */
    public void button2Click() {
        new ToastDialog(getContext()).setText("Moust Mam has registed? " + RxBus.get().hasRegistered(mMouseMam)).show();
    }

    /**
     * showToast
     *
     * @param mouse mouse
     */
    public void showToast(Mouse mouse) {
        Text text = new Text(this);
        text.setWidth(Constants.WIDTH);
        text.setHeight(Constants.HEIGHT);
        text.setTextSize(Constants.TEXT_SIZE);
        text.setText(mouse.toString());

        ShapeElement style = new ShapeElement();
        style.setShape(ShapeElement.RECTANGLE);
        style.setRgbColor(RgbColor.fromArgbInt(Constants.TOAST_BG));
        text.setBackground(style);

        DirectionalLayout mainLayout = new DirectionalLayout(this);
        mainLayout.addComponent(text);

        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setComponent(mainLayout);
        toastDialog.setAlignment(TextAlignment.CENTER);
        toastDialog.setSize(Constants.WIDTH, Constants.HEIGHT);
        toastDialog.show();
    }

    /**
     * Unregister the register object.
     */
    @Override
    protected void onStop() {
        com.hwangjr.rxbus.RxBus.get().unregister(mMouseMam);
        ArrayList<Cat> cats = mCatMam.getCats();
        for (Cat cat : cats) {
            RxBus.get().unregister(cat);
        }
        super.onStop();
    }
}
